package com.strato.base.bean.v1_0_0;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BeanUtil {

	public static List<Field> getFields(Class<?> clazz){
		List<Field> list=new ArrayList<Field>();
		while(clazz!=null){
			list.addAll(Arrays.asList(clazz.getDeclaredFields()));
			clazz=clazz.getSuperclass();
		}
		return list;
	}
	
	public static Field getField(Class<?> clazz,String fieldName){
		while(clazz!=null){
			try {
				Field field = clazz.getDeclaredField(fieldName);
				if(field!=null){
					return field;
				}
			} catch (NoSuchFieldException e) {
			} catch (SecurityException e) {
			}
			clazz=clazz.getSuperclass();
		}
		return null;
	}
	
	public static List<Method> getMethods(Class<?> clazz){
		List<Method> list=new ArrayList<Method>();
		while(clazz!=null){
			list.addAll(Arrays.asList(clazz.getDeclaredMethods()));
			clazz=clazz.getSuperclass();
		}
		return list;
	}
	
	public static Method getMethod(Class<?> clazz,String methodName,Class<?>[] parameterTypes){
		while(clazz!=null){
			try {
				Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
				if(method!=null){
					return method;
				}
			} catch (NoSuchMethodException e) {
			} catch (SecurityException e) {
			}
			clazz=clazz.getSuperclass();
		}
		return null;
	}
	
	public static List<Class<?>> getInterfaces(Class<?> clazz){
		List<Class<?>> list=new ArrayList<Class<?>>();
		while(clazz!=null){
			list.addAll(Arrays.asList(clazz.getInterfaces()));
			clazz=clazz.getSuperclass();
		}
		return list;
	}
}
